<?php
namespace Api\Controller;
use Think\Controller;
class InfoController extends BaseController{
  /**
  *获取所有展览 get
  *@param: 
  *@return:
  */
  public function getAllExhibition(){
    $type = I('get.type');
    if($type==null){
      $this->response(array('info'=>'请传活动类型'),json,400);
    }
    $exhibitions = M('exhibition')
    ->where(array('type'=>$type))
    ->join('picture p1 on p1.id = exhibition.banner','left')
    ->join('picture p2 on p2.id = exhibition.cover','left')
    ->field('exhibition.*,p1.path as banner_path,p2.path as cover_path')
    ->order('begin_date desc')
    ->select();
    $this->response(array('info'=>$exhibitions),json,200);
  }

  /**
  *获取展览详情跟票务信息 get
  *@param: 
  *@return:
  */
  public function getExhibitionDetail(){
    $exhibitionId = I('get.exhibitionId');
    if(!$exhibitionId){
      $this->response(array('info'=>'缺少参数'),json,400);
    }
    $exhibition = M('exhibition')
    ->field('exhibition.*,p1.path as banner_path,p2.path as cover_path')
    ->where(array('exhibition.id'=>$exhibitionId))
    ->join('picture p1 on p1.id = exhibition.banner','left')
    ->join('picture p2 on p2.id = exhibition.cover','left')
    ->find();

    $tickets = M('ticket')
    ->where(array('exhibition_id'=>$exhibitionId,'status'=>1))
    ->order('sort asc')
    ->select();

    //统计支付成功数量
    $paied_num = M('ticket_order')
    ->join('ticket on ticket.id = ticket_order.ticket_id','left')
    ->where(array('ticket.exhibition_id'=>$exhibitionId,'ticket_order.status'=>1))
    ->field('sum(amount) as count')
    ->select();

    //获取会员信息
    $uid = cookie('visitor_id');
    // $uid = '226';
    if($uid){
      $user = M('visitor')->find($uid);
      $vip_check=M('vip_order')
      ->where(array('visitor_id'=>$uid))
      ->join('membership on membership.id = vip_order.vip_type','left')
      ->field('vip_order.*,membership.title as vip_title,membership.title_en as vip_title_en')
      ->order('end_time desc')
      ->find();

      if($vip_check&&$vip_check['end_time']>time()){
        $user['vip'] = true;
      }
    }

    $exhibition['tickets'] = $tickets;
    $exhibition['paid'] = intval($paied_num[0]['count']);
    $exhibition['limit'] = intval($exhibition['limit']);
    $exhibition['user'] = $user;
    $this->response(array('info'=>$exhibition),json,200);
  }

  /**
  *获取所有展览作品 get
  *@param: categoryId(如果不传则显示普通作品)
  *@return:
  */
  public function getAllworks(){
    $page = I('get.page')?I('get.page'):1;
    $categoryId = I('get.categoryId')?I('get.categoryId'):10;
    $map['works.category'] = array('like','%'.$categoryId.'%');
    $works = M('works')
    ->field('works.*,picture.path as work_path,file.savepath as file_savepath,file.savename as file_savename,artist.name as artist_name,artist.name_en as artist_name_en')
    ->where(array('works.status'=>0))
    ->where($map)
    ->join('picture on picture.id = works.picture','left')
    ->join('file on file.id = works.audio','left')
    ->join('artist on artist.id = works.artist','left')
    ->order('works.sort asc')
    ->page($page,10)
    ->select();
    //获取用户关注情况
    $subscribe = $this->getUserSession();
    $this->response(array('info'=>$works,'subscribe'=>$subscribe),json,200);
  }

  /**
  *获取所有作品详情 get
  *@param: workId
  *@return:
  */
  public function getWorkDetail(){
    $workId = I('get.workId');
    if(!$workId){
      $this->response(array('info'=>'缺少参数'),json,400);
    }
    $work = M('works')
    ->field('works.*,f1.savepath as f1_savepath,f1.savename as f1_savename,f2.savepath as f2_savepath,f2.savename as f2_savename,artist.name as artist_name,artist.name_en as artist_name_en')
    ->where(array('works.id'=>$workId))
    ->join('file f1 on f1.id = works.audio','left')
    ->join('file f2 on f2.id = works.audio_en','left')
    ->join('artist on artist.id = works.artist','left')
    ->find();
    $pics = explode(',',$work['picture']);
    $work['work_path'] = array();
    if(count($pics)>0){
      foreach ($pics as $key => $value) {
        # code...
        $pic = M('picture')->find($value);
        if($pic){
          array_push($work['work_path'],$pic['path']);
        }
      }
    }
    $work['audio_path'] = '/Uploads/Download/'.$work['f1_savepath'].$work['f1_savename'];
    $work['audio_en_path'] = '/Uploads/Download/'.$work['f2_savepath'].$work['f2_savename'];
    $work['attribute'] = json_decode($work['attribute']);

    //获取用户关注情况
    $subscribe = $this->getUserSession();

    $this->response(array('info'=>$work,'subscribe'=>$subscribe),json,200);
  }

  /**
  *获取banner
  *@param: 
  *@return:
  */
  public function getIndexBanner(){
    $model = I('get.model')?I('get.model'):1;
    $banners = M('banner')
    ->field('banner.*,picture.path as cover_path')
    ->where(array('is_delete'=>1,'bannermodel'=>$model))
    ->join('picture on picture.id = banner.cover')
    ->order('sort asc')
    ->select();
    $this->response(array('info'=>$banners),json,200);
  }

}